/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function initNV() {
  document.title = 'SEPIA: '+msg( lang, 'msg_nv' );
  
  loadHeaderSubSub( lang );

  loadTopicList('nv');
  
  loadMessages(lang);
}

function loadTopicNV( topicId ) {
  lastTopicId["nv"] = topicId;
  if ( topicId=='' ) {
    jQuery('#additional').slideUp("slow",footer);
  } else {
    jQuery('#additional').slideUp("fast", function(){
      var param = eval('$H({"action":"GetTopic","topicId": ' + topicId + ', "tool":"nv"})').toQueryString();  
      var json = sync(param);
      loadTopicJsonIntoHTML(json);
      loadVotingNugget(json);
      checkVotedAnswerNugget(json);
      jQuery('#additional').slideDown("slow",footer);
    });
  }
}

function loadVotingNugget(json) {
    var table = '<table width="100%"  border="0" cellspacing="0" cellpadding="3">\n';
    var nuggets = json.topics[0].answerNuggets;
    var counter = 0;
    for (var i = 0; nuggets && i < nuggets.length; i++) {
      if ( nuggets[i].isSimilar==1 ) continue;
      var id = nuggets[i].nuggetId;
        var tr = '<tr valign="top" bgcolor="'+((counter++%2==1)?'#95FFD5':'#D5FFEE"')+'">\n';
    tr += '<td width="25"><input type="checkbox" id="nugget'+id+'" name="votes" value="'+id+'"></td>\n';
    tr += '<td><label for="nugget'+id+'">'+nuggets[i].answerNugget+'</label></td>\n';
    tr += '</tr>\n';
    table += tr;
    }
    table += '</table>\n';
  $('answerNugget').innerHTML = table;
}

function checkVotedAnswerNugget(json) {
    var nuggets = json.topics[0].answerNuggets;
    for (var i = 0; nuggets && i < nuggets.length; i++) {
      if ( nuggets[i].isSimilar==1 ) continue;
      var votes = nuggets[i].votes;
      for (var j = 0; votes && j < votes.length; j++) {
        if ( votes[j].userId == userId ) {
          $('nugget'+nuggets[i].nuggetId).checked = votes[j].isVoted==1?true:false;
        }
      }
    }
}

function saveVotedAnswerNugget() {
  var param = [];
  param.action  = "UpdateVote";
  param.topicId  = $('topicId').value;
  param.userId  = userId;
  
  var inputs = document.getElementsByTagName('input');
  
  var counter=0;
  var ids="";
  for (var i=0; i < inputs.length; i++) {
    if (inputs[i].type=='checkbox' && inputs[i].checked==true) {
      ids += (counter++>0?"_":"") + inputs[i].value;
    }
  }
  param.pNuggetIds = ids;
  counter=0;
  ids="";
  for (var i=0; i < inputs.length; i++) {
    if (inputs[i].type=='checkbox' && inputs[i].checked==false) {
      ids += (counter++>0?"_":"") + inputs[i].value;
    }
  }
  param.nNuggetIds = ids;
  
  sync(param);
  var topic = $('topicList').options[$('topicList').selectedIndex].innerHTML;
  if ( topic.match( "\\[x\\]" )!="[x]" ) {
    $('topicList').options[$('topicList').selectedIndex].innerHTML = done_prefix+topic;
  }
  log("SAVE VOTE","topic id="+$('topicId').value);
  showMessage( msg_suc_nug_save );
}
